package com.cy.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 简单的日期工具
 */
public final class SimpleDateUtils {

    private SimpleDateUtils() {}

    /**
     *  获取该日期所在的季度
     *  1 第一季度 2 第二季度 3 第三季度 4 第四季度
     *
     * @param date
     * @return
     */
    public static int getSeason(Calendar date) {
        int season = 0;
        int month = date.get(Calendar.MONTH);
        switch (month) {
            case Calendar.JANUARY:
            case Calendar.FEBRUARY:
            case Calendar.MARCH:
                season = 1;
                break;
            case Calendar.APRIL:
            case Calendar.MAY:
            case Calendar.JUNE:
                season = 2;
                break;
            case Calendar.JULY:
            case Calendar.AUGUST:
            case Calendar.SEPTEMBER:
                season = 3;
                break;
            case Calendar.OCTOBER:
            case Calendar.NOVEMBER:
            case Calendar.DECEMBER:
                season = 4;
                break;
            default:
                break;
        }
        return season;
    }

    /**
     * 获取一年有几个周
     * @param year
     * @return
     */
    public static int getWeekNumOfYear(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        return calendar.getActualMaximum(Calendar.WEEK_OF_YEAR);
    }

    public final static SimpleDateFormat HAS_YEAR = new SimpleDateFormat("yyyy");
    public final static SimpleDateFormat HAS_MONTH = new SimpleDateFormat("yyyy-MM");
    public final static SimpleDateFormat HAS_DAY = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 获取时间的最大值
     * @param dateStr 时间格式字符串
     * @param outputFormat 输出格式
     * @return
     */
    public static String getMaxTime(String dateStr, String outputFormat){
        SimpleDateFormat[] formats = new SimpleDateFormat[]{HAS_DAY, HAS_MONTH, HAS_YEAR};
        String pattern = null;
        Calendar calendar = Calendar.getInstance();
        for (SimpleDateFormat format : formats){
            try {
                calendar.setTime(format.parse(dateStr));
                pattern = format.toPattern();
                break;
            } catch (ParseException e) {}
        }

        if (pattern == null)
            return null;

        if (!pattern.contains("MM"))
            calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
        if (!pattern.contains("dd"))
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        return new SimpleDateFormat(outputFormat).format(calendar.getTime());
    }

    /**
     * 获取时间的最小值
     * @param dateStr 时间格式字符串
     * @param outputFormat 输出格式
     * @return
     */
    public static String getMinTime(String dateStr, String outputFormat){
        SimpleDateFormat[] formats = new SimpleDateFormat[]{HAS_DAY, HAS_MONTH, HAS_YEAR};
        String pattern = null;
        Calendar calendar = Calendar.getInstance();
        for (SimpleDateFormat format : formats){
            try {
                calendar.setTime(format.parse(dateStr));
                pattern = format.toPattern();
                break;
            } catch (ParseException e) {}
        }

        if (pattern == null)
            return null;

        if (!pattern.contains("MM"))
            calendar.set(Calendar.MONTH, calendar.getActualMinimum(Calendar.MONTH));
        if (!pattern.contains("dd"))
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
        return new SimpleDateFormat(outputFormat).format(calendar.getTime());
    }


    public static void main(String[] args) {
        String lastTime = getMinTime("2011-12", "yyyy-MM-dd HH:mm:ss");
        System.out.println(lastTime);
    }

}
